package com.bdqn.controller;

import com.bdqn.entity.User;
import com.bdqn.service.UserService;
import com.bdqn.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户控制器
 */
@Controller
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;

    /**
     * 测试方法
     */
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        return "Hello SSM Project! 当前时间: " + new java.util.Date();
    }

    /**
     * 首页
     */
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
    
    /**
     * 用户列表页面
     */
    @RequestMapping("/list")
    public String userList(Model model) {
        try {
            List<User> users = userService.getAllUsers();
            int userCount = userService.getUserCount();
            
            model.addAttribute("users", users);
            model.addAttribute("userCount", userCount);
            model.addAttribute("message", "用户数据加载成功");
            
            return "userList";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "数据加载失败: " + e.getMessage());
            return "error";
        }
    }
    
    /**
     * 获取用户列表（Ajax）
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public AjaxResult getUserList() {
        try {
            List<User> users = userService.getAllUsers();
            return AjaxResult.success("查询成功", users);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询用户
     */
    @RequestMapping("/getUserById")
    @ResponseBody
    public AjaxResult getUserById(@RequestParam("id") Integer id) {
        try {
            User user = userService.getUserById(id);
            if (user != null) {
                return AjaxResult.success("查询成功", user);
            } else {
                return AjaxResult.error("用户不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 初始化测试数据
     */
    @RequestMapping("/initData")
    @ResponseBody
    public AjaxResult initTestData() {
        try {
            boolean success = userService.initTestData();
            if (success) {
                return AjaxResult.success("测试数据初始化成功");
            } else {
                return AjaxResult.error("测试数据初始化失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("初始化失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取用户统计信息
     */
    @RequestMapping("/getStats")
    @ResponseBody
    public AjaxResult getUserStats() {
        try {
            int userCount = userService.getUserCount();
            return AjaxResult.success("统计成功", "当前用户总数: " + userCount);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("统计失败: " + e.getMessage());
        }
    }
}